##=============================================================================
## Appendix: Table 1 (cont'd on Table 2)
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse", "texreg")


ipak(packages)

##---------
# Load data
#setwd("~/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/final_version_oct_2024/replication-scripts")
load("clean_ukraine_data.RData")

##----------------------------
# Descriptive Respondent Stats
dat <- dat %>%
  select(wave, sex, age, ethnic, home_lang, married, edu, job, finances, 
         displaced) %>% 
  mutate(wave = ifelse(wave == 0, "July 2022", "February 2023"),
         sex = ifelse(sex == 0, "Female", "Male"),
         age_group = case_when(age >= 18 & age < 30 ~ "18-29",
                               age >= 30 & age < 45 ~ "30-44",
                               age >= 45 & age < 60 ~ "45-59",
                               age >= 60 & age < 75 ~ "60-74",
                               age >= 75 ~ "75+"),
         ethnic = case_when(ethnic == 1 ~ "Ukrainian",
                            ethnic == 2 ~ "Russian",
                            ethnic == 3 ~ "Ukrainian or Russian (self-report)",
                            ethnic == 4 ~ "Belarussian",
                            ethnic == 5 ~ "Moldavian",
                            ethnic == 6 ~ "Crimean Tarar",
                            ethnic == 7 ~ "Bulgarian",
                            ethnic == 8 ~ "Hungarian",
                            ethnic == 9 ~ "Romanian",
                            ethnic == 10 ~ "Pole",
                            ethnic == 11 ~ "Jew",
                            ethnic == 12 ~ "Other",
                            ethnic == 13 ~ "Refused"),
         home_lang = case_when(home_lang == 1 ~ "Ukrainian",
                               home_lang == 2 ~ "Russian",
                               home_lang == 3 ~ "Equally, but more often Ukrainian",
                               home_lang == 4 ~ "Other",
                               home_lang == 7 ~ "Hard to Say",
                               home_lang == 9 ~ "Refused"),
         married = case_when(married == 1 ~ "Single",
                             married == 2 ~ "Married or Living with Partner",
                             married == 3 ~ "Divorced or Widowed",
                             married == 4 ~ "Refused"),
         edu = case_when(edu == 1 ~ "Primary (less than 7 grades)",
                         edu == 2 ~ "Incomplete secondary (less than 10 grades)",
                         edu == 3 ~ "RU, FZU, PTU after 7-8 grades",
                         edu == 4 ~ "Complete general secondary",
                         edu == 5 ~ "SPTU, PTU after 10-11 grades",
                         edu == 6 ~ "Vocational Secondary School",
                         edu == 7 ~ "Some College",
                         edu == 8 ~ "College",
                         edu == 97 ~ "Don't Know/ Refused"),
         job = case_when(job == 1 ~ "Worker or Farmer",
                         job == 2 ~ "Servant (without higher education",
                         job == 3 ~ "Professional (with higher education",
                         job == 4 ~ "Self-Employed",
                         job == 5 ~ "Entrepreneur or Farmer",
                         job == 6 ~ "Military",
                         job == 7 ~ "Stay at Hoe",
                         job == 8 ~ "Pensioner (age or disability)",
                         job == 9 ~ "Student",
                         job == 10 ~ "Unemployed",
                         job == 11 ~ "Other",
                         job == 97 ~ "Don't Know / Refused"),
         finances = case_when(finances == 1 ~ "We do not have enough money even for food.",
                              finances == 2 ~ "We have enough money but only for basic needs.",
                              finances == 3 ~ "We have enough money for daily expenses.",
                              finances == 4 ~ "Usually, we have enough money, but cannot buy expensive things.",
                              finances == 5 ~ "We can afford expensive purchases without too much difficulty.",
                              finances == 6 ~ "We can buy a car, but buying a home is difficult.",
                              finances == 7 ~ "We can afford anything we want.",
                              finances == 97 ~ "Don't Know",
                              finances == 98 ~ "Refused"),
         displaced = ifelse(displaced == 0, "No", "Yes")) %>% 
  select(-age)
  
dat <- dat %>%  
  pivot_longer(cols = -wave, names_to = "variable", values_to = "category") %>%
  group_by(wave, variable, category) %>%
  summarize(
    n = n(),
    .groups = "drop_last"
  ) %>%
  mutate(
    proportion = n / sum(n)
  ) %>%
  ungroup() %>% 
  mutate(proportion = round(proportion, digits = 2))

dat <- dat %>% 
  pivot_wider(names_from = wave, values_from = c(n, proportion),
              names_glue = "{wave}_{.value}") %>% 
  mutate(variable = case_when(
    variable == "sex" ~ "Gender", 
    variable == "age_group" ~ "Age Group",
    variable == "ethnic" ~ "Ethnicity",
    variable == "home_lang" ~ "Home Language",
    variable == "married" ~ "Marital Status",
    variable == "edu" ~ "Education",
    variable == "job" ~ "Employment",
    variable == "finances" ~ "Income",
    variable == "displaced" ~ "Displaced")) %>% 
  select(variable, category, `July 2022_n`, `July 2022_proportion`,
         `February 2023_n`, `February 2023_proportion`)


kableExtra::kable(
  dat,
  col.names = c("Variable", "Category", "Count", "Proportion", "Count", 
                "Proportion"),
  align = c("l", "c", "c", "c", "c", "c"),
  digits = 2,
  caption = "Caption"
) %>%
  kableExtra::add_header_above(
    header = c(" " = 1, " " = 1, "July 2022 ($n=2,000$)" = 2, 
               "February 2023 ($n=2,007$)" = 2)
  ) %>%
  kableExtra::footnote(
    general = "",
    general_title = "Note.",
    footnote_as_chunk = TRUE
  )

rm(list = ls())
##=============================================================================
## End of File
##=============================================================================